package com.health.db.service.sm.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.dao.sm.IGoodsGiftListDao;
import com.health.db.dao.sm.IGoodsListDao;
import com.health.db.dao.sm.IGoodsOrderContactDao;
import com.health.db.dao.sm.IGoodsOrderDao;
import com.health.db.dao.user.IUserAccountJournalDao;
import com.health.db.dao.user.IUserAccountLedgerDao;
import com.health.db.dao.user.IUserLISTDao;
import com.health.db.model.GoodsGiftList;
import com.health.db.model.GoodsGiftListExample;
import com.health.db.model.GoodsList;
import com.health.db.model.GoodsOrder;
import com.health.db.model.GoodsOrderContact;
import com.health.db.model.GoodsOrderExample;
import com.health.db.model.UserAccountJournal;
import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserLIST;
import com.health.db.service.sm.IGoodsOrderService;
import com.health.mapp.utils.Constant;

public class GoodsOrderServiceImpl implements IGoodsOrderService {

	private IGoodsOrderDao goodsOrderDao ;
	
	private IGoodsListDao goodsListDao ;

	private IUserAccountLedgerDao userAccountLedgerDao ;
	
	private IUserAccountJournalDao userAccountJournalDao ;
	
	private IUserLISTDao userLISTDao;
	
	@Autowired
	private IGoodsGiftListDao goodsGiftListDao;
	
	@Autowired
	private IGoodsOrderContactDao goodsOrderContactDao;
	
	@Override
	public GoodsOrder insert(GoodsOrder record) {
		// TODO Auto-generated method stub
		
		GoodsList goods = goodsListDao.selectByGoodsId(record.getGoodsid());
		
		if (null!=goods&&(Constant.CONSTANTS_PAY_ACTION_ID_41.intValue()==goods.getTypeid().intValue()
							||Constant.CONSTANTS_PAY_ACTION_ID_51.intValue()==goods.getTypeid().intValue()
							||Constant.CONSTANTS_PAY_ACTION_ID_61.intValue()==goods.getTypeid().intValue())) {
			record.setGoodsName(goods.getGoodsName());
			record.setGoodsType(String.valueOf(goods.getTypeid()));
			record.setTotal(goods.getSalePrice());
			record.setGoodsMeasure(goods.getGoodsMeasure());
			record.setGiftAmount(goods.getSaleListPrice().subtract(goods.getSalePrice()));
			goodsOrderDao.insert(record);
			record.setGoodsList(goods);
		}
		return record;
	}
	
	/**
	 *课程赠品专用
	 */
	@Override
	public GoodsOrder insertCourse(GoodsOrder record,GoodsOrderContact contactRecord){
	GoodsList goods = goodsListDao.selectByGoodsId(record.getGoodsid());
		
	if (null!=goods&&(Constant.CONSTANTS_PAY_ACTION_ID_41.intValue()==goods.getTypeid().intValue()
						||Constant.CONSTANTS_PAY_ACTION_ID_51.intValue()==goods.getTypeid().intValue()
						||Constant.CONSTANTS_PAY_ACTION_ID_61.intValue()==goods.getTypeid().intValue())) {
		record.setGoodsName(goods.getGoodsName());
		record.setGoodsType(String.valueOf(goods.getTypeid()));
		record.setTotal(goods.getSalePrice());
		record.setGoodsMeasure(goods.getGoodsMeasure());
		record.setGiftAmount(goods.getSaleListPrice().subtract(goods.getSalePrice()));
		goodsOrderDao.insert(record);
		if (null!=contactRecord) {
			contactRecord.setOrderId(record.getRecid());
			
			//查询赠品的id
			if(StringUtils.isNoneEmpty(contactRecord.getChannel())){
				
				GoodsGiftListExample example = new GoodsGiftListExample();
				example.setChannel(contactRecord.getChannel());
				example.setGoodsId(record.getGoodsid());
				GoodsGiftList gift  = goodsGiftListDao.selectByChannel(example);
				if (null!=gift) {
					contactRecord.setGiftId(String.valueOf(gift.getRecId()));
				}
			}
			
			goodsOrderContactDao.insert(contactRecord);
		}
		
		record.setGoodsList(goods);
	}
		return record;
		
	}

	public IGoodsOrderDao getGoodsOrderDao() {
		return goodsOrderDao;
	}

	public void setGoodsOrderDao(IGoodsOrderDao goodsOrderDao) {
		this.goodsOrderDao = goodsOrderDao;
	}

	public IGoodsListDao getGoodsListDao() {
		return goodsListDao;
	}

	public void setGoodsListDao(IGoodsListDao goodsListDao) {
		this.goodsListDao = goodsListDao;
	}
	

	public IGoodsGiftListDao getGoodsGiftListDao() {
		return goodsGiftListDao;
	}

	public void setGoodsGiftListDao(IGoodsGiftListDao goodsGiftListDao) {
		this.goodsGiftListDao = goodsGiftListDao;
	}

	@Override
	public int update(GoodsOrder record) {
		// TODO Auto-generated method stub
		return goodsOrderDao.update(record);
	}

	@Override
	public GoodsOrder selectByPrimaryKey(Long recid) {
		// TODO Auto-generated method stub
		return goodsOrderDao.selectByPrimaryKey(recid);
	}
	
	public IUserAccountLedgerDao getUserAccountLedgerDao() {
		return userAccountLedgerDao;
	}

	public void setUserAccountLedgerDao(IUserAccountLedgerDao userAccountLedgerDao) {
		this.userAccountLedgerDao = userAccountLedgerDao;
	}

	public IUserLISTDao getUserLISTDao() {
		return userLISTDao;
	}

	public void setUserLISTDao(IUserLISTDao userLISTDao) {
		this.userLISTDao = userLISTDao;
	}
	
	public IUserAccountJournalDao getUserAccountJournalDao() {
		return userAccountJournalDao;
	}

	public void setUserAccountJournalDao(
			IUserAccountJournalDao userAccountJournalDao) {
		this.userAccountJournalDao = userAccountJournalDao;
	}

	/**
	 * @param record订单信息
	 * @param userAccountLedger 用户账户信息
	 * @param type 支付方式
	 * @param out_trade_no 平台订单号   
	 */
	@Override
	public GoodsOrderExample updateOrder4Payment(GoodsOrder  record,UserAccountLedger userAccountLedger,int type,String out_trade_no) {
 
		GoodsOrderExample order = new GoodsOrderExample();

		goodsOrderDao.update(record);

		if (type == 0) {
			userAccountLedgerDao.insertAccountLedger(userAccountLedger);
		} else {
			userAccountLedgerDao.updateBalance(userAccountLedger);
		}

		UserAccountJournal accountJournal = new UserAccountJournal();
		// 支付成功
		if (record.getPayTypeId().shortValue() == Constant.CONSTANTS_PAYMENT_WEIXIN) {
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_2+ "-" + Constant.CONSTANTS_VOUCHERNAME_41 + "订单号"
					+ out_trade_no);
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_41+ "-" + Constant.CONSTANTS_VOUCHERNAME_2);
		} else if (record.getPayTypeId().shortValue() == Constant.CONSTANTS_PAYMENT_ALIPAY) {
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_4+ "-" + Constant.CONSTANTS_VOUCHERNAME_41 + "订单号"
					+ out_trade_no);
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_41+ "-" + Constant.CONSTANTS_VOUCHERNAME_4);
		}
		accountJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_41);
		// 商户订单号 作为备注
		accountJournal.setComment(out_trade_no);

		delayUpdateUserAccountJournal(accountJournal, record,
				record.getOwnerid(), userAccountLedger);

		return order;
	 
	}
	
	
	/**
	 * @param record订单信息
	 * @param userAccountLedger 用户账户信息
	 * @param type 支付方式
	 * @param out_trade_no 平台订单号   
	 */
	@Override
	public GoodsOrderExample updateOrder4Payment(GoodsOrder  record,String out_trade_no) {
 
		GoodsOrderExample order = new GoodsOrderExample();

		goodsOrderDao.update(record);

		UserAccountJournal accountJournal = new UserAccountJournal();
		// 支付成功
		if (record.getPayTypeId().shortValue() == Constant.CONSTANTS_PAYMENT_WEIXIN) {
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_2+ "-" + Constant.CONSTANTS_VOUCHERNAME_41 + "订单号"
					+ out_trade_no);
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_41+ "-" + Constant.CONSTANTS_VOUCHERNAME_2);
		} else if (record.getPayTypeId().shortValue() == Constant.CONSTANTS_PAYMENT_ALIPAY) {
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_4+ "-" + Constant.CONSTANTS_VOUCHERNAME_41 + "订单号"
					+ out_trade_no);
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_41+ "-" + Constant.CONSTANTS_VOUCHERNAME_4);
		}

		// 获取商品类型
		if (StringUtils.isNotBlank(record.getGoodsType())) {
			accountJournal.setSourceid(Integer.valueOf(record.getGoodsType()));
		}

		// 商户订单号 作为备注
		accountJournal.setComment(out_trade_no);

		delayUpdateUserAccountJournal(accountJournal, record,
				record.getOwnerid(), null);

		return order;
	}
	
	
	
	
	/**
	 * 延时处理更新user表
	 * 支付回调调用
	 * accountJournal 用户记录
	 */
	private void delayUpdateUserAccountJournal(UserAccountJournal accountJournal,GoodsOrder  record,
			long userId, UserAccountLedger accountLedger) {

			// 查看用户表相关信息
			//Long docId = userId;
			UserLIST userLIST = userLISTDao.selectByUserId(userId);

			// 账单日志表
			//日记账记录
			if (null!=accountLedger) {
				accountJournal.setAcctpageid(accountLedger.getAcctpageid());
			}
			
			accountJournal.setAccountid(userId);
			accountJournal.setAccountIdno(userLIST.getUserNo());
			accountJournal.setAccountName(userLIST.getUserName());
			accountJournal.setVoucherid(record.getRecid().toString());
			//accountJournal.setJournalDesc(orderInfoTemp.getUserName()+ orderInfoTemp.getOrderTypeDesc());
			//accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_1);
			accountJournal.setJournalTotal(record.getTotal());
			//accountJournal.setJournalTotal(orderInfoTemp.getRegisterPayoffTotal());
			accountJournal.setCreateUserid(userId);
			accountJournal.setUpdateUserid(userId);
			accountJournal.setVoucherDate(record.getCreateDate());
			//充值
			accountJournal.setVouchertypeid(41);

			userAccountJournalDao.insert(accountJournal);
		}

	@Override
	public List<GoodsOrder> selectByUserId(Long userId, Long goodsId) {
		// TODO Auto-generated method stub

		if (userId.longValue()==0) {
			return new  ArrayList<GoodsOrder>();
		}
		
		GoodsOrderExample example =  new GoodsOrderExample();
		
		example.setOwnerid(userId);
		example.setGoodsid(goodsId);
		example.setRunstatusid(Constant.CONSTANTS_ORDER_RUNSTATUS_ID_5);
		
		return goodsOrderDao.selectByUserId(example);
	}

	@Override
	public List<GoodsOrder> selectByUserIdAndType(GoodsOrderExample example) {
		// TODO Auto-generated method stub
		return goodsOrderDao.selectByUserIdAndType(example);
	}

	public IGoodsOrderContactDao getGoodsOrderContactDao() {
		return goodsOrderContactDao;
	}

	public void setGoodsOrderContactDao(IGoodsOrderContactDao goodsOrderContactDao) {
		this.goodsOrderContactDao = goodsOrderContactDao;
	}
	
}
